//
//  XYBottomView.m
//  TheDragon
//
//  Created by wingfac on 2017/8/2.
//  Copyright © 2017年 LJ. All rights reserved.
//

#import "XYBottomView.h"
#import "XYButton.h"
@interface XYBottomView()
@property (nonatomic, strong) NSMutableArray *buttonsArr;
@end
@implementation XYBottomView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = getColor(whiteColor);
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
        lineView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:lineView];
    }
    return self;
}
- (void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    _buttonsArr = @[].mutableCopy;
    for (int i = 0; i < _dataArray.count; i++) {
        XYButton *button = [XYButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(SCREEN_WIDTH/_dataArray.count * i, 1, SCREEN_WIDTH/_dataArray.count, self.frame.size.height-1);
        [button setMyButtonIcon:_dataArray[i][@"img"] titleTest:_dataArray[i][@"title"] titleColor:[UIColor blackColor]];
        button.tag = 10000+i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        
        [_buttonsArr addObject:button];
    }
    
}
-(void)buttonAction:(XYButton *)sender
{
//    if([_dataArray[sender.tag - 10000][@"title"] isEqualToString:@"收藏"]){
//        if(sender.selected){
//            [sender clean];
//            sender.selected=NO;
//            [sender setMyButtonIcon:_dataArray[sender.tag - 10000][@"img"] titleTest:_dataArray[sender.tag - 10000][@"title"] titleColor:[UIColor blackColor]];
//        }
//        else if(!sender.selected){
//            [sender clean];
//            sender.selected=YES;
//            [sender setMyButtonIcon:_dataArray[sender.tag - 10000][@"img_s"] titleTest:_dataArray[sender.tag - 10000][@"title"] titleColor:getColor(mainColor)];
//        }
//    }
    if([_dataArray[sender.tag - 10000][@"title"] isEqualToString:@"解释"]){
        [sender clean];
        sender.selected=YES;
        [sender setMyButtonIcon:_dataArray[sender.tag - 10000][@"img_s"] titleTest:_dataArray[sender.tag - 10000][@"title"] titleColor:getColor(mainColor)];
    }
    else{
        
    }

    if (self.delegate && [self.delegate respondsToSelector:@selector(bottomButtonClick:)]) {
        [self.delegate bottomButtonClick:sender.tag];
    }

}
// status 1--收藏 2--未收藏
- (void)reload:(int) collectState{
    
        for(XYButton *button in _buttonsArr){
            if([_dataArray[button.tag - 10000][@"title"] isEqualToString:@"收藏"]){
                if(collectState == 1){
                    [button clean];
                    button.selected=YES;
                    [button setMyButtonIcon:_dataArray[button.tag - 10000][@"img_s"] titleTest:_dataArray[button.tag - 10000][@"title"] titleColor:getColor(mainColor)];
                }
                else{
                    [button clean];
                    button.selected=NO;
                    [button setMyButtonIcon:_dataArray[button.tag - 10000][@"img"] titleTest:_dataArray[button.tag - 10000][@"title"] titleColor:[UIColor blackColor]];
                }
            }
            else if([_dataArray[button.tag - 10000][@"title"] isEqualToString:@"解释"]){
                [button clean];
                button.selected=NO;
                [button setMyButtonIcon:_dataArray[button.tag - 10000][@"img"] titleTest:_dataArray[button.tag - 10000][@"title"] titleColor:[UIColor blackColor]];
            }
            
        }
    
}
@end
